#define  _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
using namespace std;
namespace MyPriorityQueue
{
	template<class T>
	struct less
	{
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};
	
	template<class T>
	struct greater
	{
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};


	template<class T, class Container = vector<T>,class Compare = less<int>>
	class priority_queue
	{
	public:
		void adjust_up(int child)
		{
			Compare com;

			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if ( _con[parent] < _con[child] )
				if ( com(_con[parent] , _con[child]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int parent)
		{
			Compare com;

			int child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size()
					&&  com(_con[child + 1] , _con[child]) )
				{
					child++;
				}
				//if (_con[parent] < _con[child] )
				if (com(_con[parent] , _con[child]) )
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void swap(T& x,T& y)
		{
			std::swap(x, y);
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size()-1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
	};

	void test_priority_queue()
	{
	//	priority_queue<int> pq;
		priority_queue<int,vector<int>,greater<int>> pq;
		pq.push(1);
		pq.push(5);
		pq.push(15);
		pq.push(58);
		pq.push(8);
		pq.push(6);

		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
}